package zzh.com.myapplication.test.view;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import zzh.com.myapplication.R;
import zzh.com.myapplication.RvAdapter;
import zzh.com.myapplication.test.TypeHistoryAdapter;

/**
 * Created by MJJ on 2015/7/25.
 */
public class UserEditAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    /**
     * 普通商品的（normalHolder）的标题集合,调用者传入
     */
    private List<String> normalGoodsTitls;
    /**
     * 头布局总数
     */
    private Activity context;
    private LayoutInflater inflater;
    private int TYPE_TOP0 = 0;//头部布局
    private int TYPE_TOP1 = 1;//头部布局
    private int TYPE_TOP2 = 2;//头部布局
    private int TYPE_TOP3 = 3;//头部布局
    private int TYPE_TOP4 = 4;//头部布局
    private int TYPE_TOP5 = 5;//头部布局
    private int TYPE_TOP6 = 6;//头部布局
    private int TYPE_TOP7 = 7;//头部布局
    private int TYPE_REFRESH = 8;//最下面的布局
    private int count = 7;
    private GridLayoutManager mystager;
    private List<String> bannerList = new ArrayList<>(); // 广告数据
    private HeaderBannerView bannerView;
    private List<String> stringList = new ArrayList<>();

    public UserEditAdapter(Activity mContext, List<String> normalGoodsTitls) {
        this.normalGoodsTitls = normalGoodsTitls;
        this.context = mContext;
        inflater = LayoutInflater.from(context);
        notifyDataSetChanged();
    }

    public void insert(String person, int position) {
        normalGoodsTitls.add(position - count, person);
        notifyItemInserted(position - count);

    }


    public String getItem(int position) {
        if (position < normalGoodsTitls.size())
            return normalGoodsTitls.get(position);
        else
            return null;
    }

    public void startBanner() {
        if (bannerView != null)
            bannerView.enqueueBannerLoopMessage();
    }

    public void stopBanner() {
        if (bannerView != null)
            bannerView.removeBannerLoopMessage();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup arg0, int viewType) {
        if (viewType == TYPE_TOP0) {
            bannerView = new HeaderBannerView(context);
            bannerList = ModelUtil.getBannerData();
            return new TypetypeHolder(bannerView.getView(bannerList));
        } else if (viewType == TYPE_TOP1) {
            View views = inflater.inflate(R.layout.textl_ayout, arg0, false);
            return new TextHolder(views);
        } else if (viewType == TYPE_TOP2) {
            View views = inflater.inflate(R.layout.rv_item_normal, arg0, false);
            return new Normal(views);
        } else if (viewType == TYPE_TOP3) {
            View views = inflater.inflate(R.layout.activity_main, arg0, false);
            return new TypetypeHolders(views);
        } else if (viewType == TYPE_TOP4) {
            View views = inflater.inflate(R.layout.not_layout, arg0, false);
            return new NoticeViewHolder(views);
        } else if (viewType == TYPE_TOP5) {
            VerticalTextview textview = new VerticalTextview(context);
            textview.startAnim(normalGoodsTitls);
            return new Normal(textview);
        } else if (viewType == TYPE_TOP6) {
            View views = inflater.inflate(R.layout.rv_item_normal, arg0, false);
            return new Normal(views);
        } else if (viewType == TYPE_TOP7) {
            View views = inflater.inflate(R.layout.notification_item, arg0, false);
            return new notiFication(views);
        } else {
            View view = inflater.inflate(R.layout.diglo, arg0, false);
            return new MyViewHolder(view);
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_TOP0;
        } else if (position == 1) {
            return TYPE_TOP1;
        } else if (position == 2) {
            return TYPE_TOP2;
        } else if (position == 3) {
            return TYPE_TOP3;
        } else if (position == 4) {
            return TYPE_TOP4;
        } else if (position == 5) {
            return TYPE_TOP5;
        } else if (position == 6) {
            return TYPE_TOP6;
        } else if (position == 7) {
            return TYPE_TOP7;
        } else {
            return TYPE_REFRESH;
        }
    }


    private void initCenterBean(TypetypeHolders holder) {
        final List<String> list = new ArrayList<>();
        list.add("小");
        list.add("中");
        list.add("大");
        list.add("好大");
        list.add("小");
        list.add("中");
        list.add("大");
        list.add("好大");
        holder.rvtype.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        TypeHistoryAdapter centerAdapter = new TypeHistoryAdapter(context, list);
        holder.rvtype.setAdapter(centerAdapter);
        centerAdapter.setmOnItemClickLitener(new RvAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int postion) {
                Toast.makeText(context, list.get(postion), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onItemLongClick(View v, int postion) {
                Toast.makeText(context, list.get(postion), Toast.LENGTH_LONG).show();
            }
        });

    }

    boolean isDragging;//判断scroll是否是用户主动拖拽
    boolean isScrolling;//判断scroll是否处于滑动中

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
        if (layoutManager instanceof GridLayoutManager) {
            mystager = ((GridLayoutManager) layoutManager);
            int firstItemPosition = mystager.findFirstVisibleItemPosition();
            Log.e("firstItemPosition", firstItemPosition + "");


        }
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        if (holder instanceof TypetypeHolders && normalGoodsTitls.size() != 0) {
            initCenterBean(((TypetypeHolders) holder));//加载中间head下面的数据源
        } else if (holder instanceof NoticeViewHolder && normalGoodsTitls.size() != 0) {
            startView(((NoticeViewHolder) holder).notice_marquee_view, stringList);
        } else if (holder instanceof TextHolder && normalGoodsTitls.size() != 0) {
            ((TextHolder) holder).textView.setText("我变颜色了");
            ((TextHolder) holder).textView.setTextColor(Color.WHITE);
        } else if (holder instanceof MyViewHolder && normalGoodsTitls.size() != 0) {
            UserEditAdapter.MyViewHolder normalHolder = (UserEditAdapter.MyViewHolder) holder;
            normalHolder.tv_title.setText(normalGoodsTitls.get(position - count) + "-------------" + normalGoodsTitls.size());
            if (mOnItemClickLitener != null) {
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnItemClickLitener.onItemClick(holder.itemView, position - count);
                    }
                });
                holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        mOnItemClickLitener.onItemLongClick(holder.itemView, position - count);
                        return true;
                    }
                });
            }
        } else {
        }
    }

    //普通的Holder
    class Normal extends RecyclerView.ViewHolder {

        public Normal(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //普通的Holder
    class NoticeViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.notice_marquee_view)
        NoticeLinearLayout notice_marquee_view;

        public NoticeViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            stringList.add("我是惠州源丰科技有限公司的android工程师...");
        }
    }

    //普通的Holder
    class notiFication extends RecyclerView.ViewHolder {
        public notiFication(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private void startView(final NoticeLinearLayout mMarQuee, final List<String> list) {
        int noticeState = mMarQuee.getNoticeState();
        switch (noticeState) {
            //公告栏正在启动状态,字幕滚动状态
            case 0x01:
            case 0x02:
                break;
            //公告栏正在消失状态
            case 0x04:
                mMarQuee.setHasNewCome(true);
                break;
            //公告栏已经消失
            case 0x00:
                if (list.size() != 0) {
                    mMarQuee.setNoticeState(0x01);
                    mMarQuee.startAnimation(context);
                }
                break;
            default:
                break;
        }
        mMarQuee.setOnAnimationEndListner(new NoticeLinearLayout.OnAnimationListner() {
            @Override
            //flag true 显示完成
            //flag false 消失完成
            public void onAnimationEndListner(boolean flag) {
                if (flag == true && mMarQuee.getNoticeState() == 0x01) {
                    if (list.size() != 0) {
                        mMarQuee.setNoticeState(0x02); //动画显示成功后载入一条公告
                        mMarQuee.setText(list.get(0));
                        list.remove(0);
                    }
                } else {
                    mMarQuee.setNoticeState(0x00); //动画消失后,发现有新进入的公告,开始进入;
                    if (mMarQuee.isHasNewCome()) {
                        mMarQuee.startAnimation(context);
                        mMarQuee.setHasNewCome(false);
                    }
                }
            }
        });

        mMarQuee.set_text_compelete_listner(new MarqueeTextView.CompleteListener() {
            @Override
            public void compelete() {
                /**
                 * 如果公告栏没有需要播放的内容,则消失
                 * 否则播放第0条消息;
                 */
                if (list.size() == 0) {
                    mMarQuee.setHasNewCome(false);
                    mMarQuee.stopAnimation();
                } else {
                    mMarQuee.setText(list.get(0));
                    list.remove(0);
                }
            }
        });
    }

    //普通的Holder
    class TextHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.textView)
        TextView textView;

        public TextHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //普通的Holder
    class TypetypeHolder extends RecyclerView.ViewHolder {

        public TypetypeHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //普通的Holder
    class TypetypeHolders extends RecyclerView.ViewHolder {
        @Bind(R.id.rv_homepageradapter_artist)
        RecyclerView rvtype;

        public TypetypeHolders(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    //普通的Holder
    class NormalHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_title)
        TextView tv_title;

        public NormalHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    @Override
    public int getItemCount() {
        return count + normalGoodsTitls.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.tv_title)
        TextView tv_title;

        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View v, int postion);

        void onItemLongClick(View v, int postion);
    }

    /**
     * 自定义条目点击监听
     */
    private RvAdapter.OnItemClickListener mOnItemClickLitener;

    public void setmOnItemClickLitener(RvAdapter.OnItemClickListener mOnItemClickLitener) {
        this.mOnItemClickLitener = mOnItemClickLitener;
    }
}

